Imports System.Xml
Imports System.IO

Public NotInheritable Class LinkFile

    Private _linksDOM As XmlDocument

    Private Const BaseDocumentXml As String = "<Links/>"
    Private Const LinkElementName As String = "Link"
    Public Const CodeAttributeName As String = "code"
    Public Const UrlAttributeName As String = "url"
    Private Const IdAttributeName As String = "id"
    Public Const RequiredProductsAttributeName As String = "requiredProducts"
    Public Const ProductNameAttributeName As String = "productName"
    Public Const PriceAttributeName As String = "price"
    Public Const ArtistsAttributeName As String = "artists"

    Private Const PRODUCT_XPATH As String = "/Links/Link[@code = '{0}']"
    Private Const MAX_ID_XPATH As String = "/Links/Link[not(@id <= preceding-sibling::Link/@id) and not(@id <= following-sibling::Link/@id)]"


    Public Sub New()

        _linksDOM = New XmlDocument()
        _linksDOM.LoadXml(BaseDocumentXml)

    End Sub

    Public Sub New(ByVal filename As String)

        _linksDOM = New XmlDocument()
        If File.Exists(My.Settings.DataDirectory & filename) Then
            _linksDOM.Load(My.Settings.DataDirectory & filename)
        Else
            _linksDOM.LoadXml(BaseDocumentXml)
        End If

    End Sub

    Public ReadOnly Property MaxId() As Integer
        Get
            Dim maxIdElement As XmlNode
            maxIdElement = _linksDOM.SelectSingleNode(MAX_ID_XPATH)
            If maxIdElement Is Nothing Then
                Return 0
            Else
                Return CType(CType(maxIdElement, XmlElement).GetAttribute(IdAttributeName), Integer)
            End If
        End Get
    End Property

    Public Sub AddLink(ByVal productCode As String, _
                        ByVal url As String, _
                        ByVal id As Integer, _
                        ByVal requiredProducts As String, _
                        ByVal productName As String, _
                        ByVal price As Single, _
                        ByVal artists As String)

        Dim foundLink As XmlNode = _linksDOM.SelectSingleNode(String.Format(PRODUCT_XPATH, productCode))

        If foundLink Is Nothing Then

            Dim linkElement As XmlElement = _linksDOM.CreateElement(LinkElementName)
            linkElement.SetAttribute(CodeAttributeName, _
                                     productCode)
            linkElement.SetAttribute(UrlAttributeName, _
                                     url)
            linkElement.SetAttribute(IdAttributeName, _
                                     id.ToString)
            linkElement.SetAttribute(RequiredProductsAttributeName, _
                                     requiredProducts)
            linkElement.SetAttribute(ProductNameAttributeName, _
                                     productName)
            linkElement.SetAttribute(PriceAttributeName, _
                                     price.ToString)
            linkElement.SetAttribute(ArtistsAttributeName, _
                                     artists)
            _linksDOM.DocumentElement.AppendChild(linkElement)

        End If

    End Sub

    Public Sub AddLink(ByVal productCode As String, _
                        ByVal url As String, _
                        ByVal id As Integer, _
                        ByVal requiredProducts As String, _
                        ByVal productName As String, _
                        ByVal price As Single)

        Dim foundLink As XmlNode = _linksDOM.SelectSingleNode(String.Format(PRODUCT_XPATH, productCode))

        If foundLink Is Nothing Then

            Dim linkElement As XmlElement = _linksDOM.CreateElement(LinkElementName)
            linkElement.SetAttribute(CodeAttributeName, _
                                     productCode)
            linkElement.SetAttribute(UrlAttributeName, _
                                     url)
            linkElement.SetAttribute(IdAttributeName, _
                                     id.ToString)
            linkElement.SetAttribute(RequiredProductsAttributeName, _
                                     requiredProducts)
            linkElement.SetAttribute(ProductNameAttributeName, _
                                     productName)
            linkElement.SetAttribute(PriceAttributeName, _
                                     price.ToString)
            _linksDOM.DocumentElement.AppendChild(linkElement)

        End If

    End Sub

    Public Sub AddLink(ByVal productCode As String, _
                        ByVal url As String, _
                        ByVal id As Integer, _
                        ByVal requiredProducts As String)

        Dim foundLink As XmlNode = _linksDOM.SelectSingleNode(String.Format(PRODUCT_XPATH, productCode))

        If foundLink Is Nothing Then

            Dim linkElement As XmlElement = _linksDOM.CreateElement(LinkElementName)
            linkElement.SetAttribute(CodeAttributeName, _
                                     productCode)
            linkElement.SetAttribute(UrlAttributeName, _
                                     url)
            linkElement.SetAttribute(IdAttributeName, _
                                     id.ToString)
            linkElement.SetAttribute(RequiredProductsAttributeName, _
                                     requiredProducts)
            _linksDOM.DocumentElement.AppendChild(linkElement)

        End If

    End Sub

    Public Sub AddLink(ByVal productCode As String, _
                        ByVal url As String, _
                        ByVal id As Integer)

        Dim foundLink As XmlNode = _linksDOM.SelectSingleNode(String.Format(PRODUCT_XPATH, productCode))

        If foundLink Is Nothing Then

            Dim linkElement As XmlElement = _linksDOM.CreateElement(LinkElementName)
            linkElement.SetAttribute(CodeAttributeName, _
                                     productCode)
            linkElement.SetAttribute(UrlAttributeName, _
                                     url)
            linkElement.SetAttribute(IdAttributeName, _
                                     id.ToString)
            _linksDOM.DocumentElement.AppendChild(linkElement)

        End If

    End Sub

    Public Sub WriteFile(ByVal filename As String)

        _linksDOM.Save(My.Settings.DataDirectory & filename)

    End Sub

End Class
